/**
# JVLib.GoogleFont - JV Libries Javascript
# @version 1.0
# @support by JVAdapter.1.1.2.js
# ------------------------------------------------------------------------
# author    Open Source Code Solutions Co
# copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
# @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
# Websites: http://www.joomlavi.com
# Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/

; (function () {
    J.GoogleFont = new Abstract({
        options: {
            maxRequest: 1,
            numberOneRequest: 50,
            tryRequest: 2
        },
        load: function () {
            addQueued.apply(this, arguments);
            load();
            return J.GoogleFont;
        },
        remove: function () {
            for (var x = 0; x < arguments.length; x++) {
                var arg = arguments[x];
                var type = $type(arg);
                switch (type) {
                    case 'string':
                        remove(arg);
                        break;
                    case 'array':
                        arg.each(function (a) {
                            J.GoogleFont.remove(a);
                        });
                        break;
                }
            }
            return J.GoogleFont;
        },
        clearQueued: function () {
            queued = [];
            J.GoogleFont.fireEvent('clearqueued');
        },
        reset: function(font){
            delete loadeds[font.trim()];
        }
    }).extend(new Options).extend(new Events);


    var loadeds = {};
    var queued = [];
    var process = 0;
    var opts = J.GoogleFont.options;
    var loadings = {};

    var getPreparation = function () {
        var preparation = [];
        var i;
        for (i = 0; i < queued.length; i++) {
            if (preparation.length >= opts.numberOneRequest) break;

            var fontName = queued[i];
            var status = loadeds[fontName];
            var type = $type(status);
            switch (type) {
                case 'boolean':
                    break;
                case 'number':
                    if (loadeds[fontName] < opts.tryRequest) {
                        if (!loadings[fontName]) {
                            loadeds[fontName]++;
                            preparation.push(fontName);
                            loadings[fontName] = true;
                        }
                    } else {
                        loadeds[fontName] = false;
                    }
                    break;
                case false:
                    loadeds[fontName] = 1; 
                    preparation.push(fontName);
                    loadings[fontName] = true;
                    break;
            }
        }
        queued.splice(0, i);
        return preparation;
    }


    var load = function () {
        if (process >= opts.maxRequest) return;
        var preparation = getPreparation();
        if (preparation.length == 0) return;                
        var callNext = function (fontName) {
            preparation.splice(preparation.indexOf(fontName), 1);
            delete loadings[fontName];
            if (preparation.length == 0) {
                process--;
                load();
            }
        }
        var WebFontConfig = {
            google: { families: preparation },
            loading: function () {
                J.GoogleFont.fireEvent('requesting', WebFontConfig.google.families);
            },
            fontloading: function (fontName, fontDescription) {
                J.GoogleFont.fireEvent('fontloading', fontName, fontDescription);
            },
            fontactive: function (fontName, fontDescription) {
                loadeds[fontName] = true;
                callNext(fontName);
                J.GoogleFont.fireEvent('fontsuccess', fontName, fontDescription);
            },
            fontinactive: function (fontName, fontDescription) {
                callNext(fontName);
                queued.push(fontName);
                J.GoogleFont.fireEvent('fontfailed', fontName, fontDescription);
            },
            active: function () {
                J.GoogleFont.fireEvent('requestsuccess', WebFontConfig.google.families);
            },
            inactive: function () {
                J.GoogleFont.fireEvent('requestfailed', WebFontConfig.google.families);
            }
        };
        process++;
        WebFont.load(WebFontConfig);
        load();
    }

    var addQueued = function () {
        for (var x = 0; x < arguments.length; x++) {
            var arg = arguments[x];
            var type = $type(arg);
            switch (type) {
                case 'string':
                    queued.push(arg.trim());
                    J.GoogleFont.fireEvent('addqueued', arg, queued);
                    break;
                case 'array':
                    arg.each(function (a) {
                        addQueued(a);
                    });
                    break;
            }
        }
    }

    var remove = function (key) {
        $$('link').each(function (link) {
            if (link.attr('href').indexOf(key.replace(/ /g, '+')) != -1)
                link.remove();
        });
    }
})();